package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {
    @Autowired
    SensitiveMapper sensitiveMapper;
    @Override
    public ResponseResult findListByQuery(SensitiveDTO dto) {
        //1.校验业务参数
        //检查分页参数
        dto.checkParam();
        //2.执行分页条件查询
        //2.1分页的条件对象
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());

        //2.2查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();

        //判断前端是否传name  ,添加模糊查询条件
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> pageResult = this.page(pageReq, queryWrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.校验参数 得到要添加的敏感词
        String sensitives = adSensitive.getSensitives();
        // 敏感词不能为空
        if (sensitives==null||"".equals(sensitives)){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        if (sensitives.length()>10){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能超过10个字");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitives));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive.getId()==null){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //1.校验参数 得到要添加的敏感词
        String sensitives = adSensitive.getSensitives();
        //根据id获得老的敏感词信息
        AdSensitive oldSensitive = this.getById(adSensitive.getId());
        // 敏感词不能为空
        if (sensitives==null||"".equals(sensitives)){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        //敏感词不能和老的一样
        if (oldSensitive.getSensitives().equals(sensitives)){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"敏感词和修改之前的一样");
        }

        if (sensitives.length()>10){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能超过10个字");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitives));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词已经存在");
        }
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //校验id
        if (id==null){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        AdSensitive sensitive = this.getById(id);
        if (sensitive==null){
            CustException.cust( AppHttpCodeEnum.PARAM_INVALID,"敏感词信息不存在");
        }
        this.removeById(id);
       // System.out.println(5/0);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(sensitiveMapper.findAllSensitives());
    }
}
